<template>
  <div class="app-container">
    <div class="filter-container-more">
      <el-input v-model="searchObj.data.id" size="medium" placeholder="识别码" class="global-search-box"/>
      <el-input v-model="searchObj.data.schemename" size="medium" placeholder="方案名称" class="global-search-box"/>
      <el-input v-model="searchObj.data.schemeclass" size="medium" placeholder="方案类别" class="global-search-box"/>
      <el-input v-model="searchObj.data.serialnumber" size="medium" placeholder="文件编码" class="global-search-box"/>
      <el-input v-model="searchObj.data.state" size="medium" placeholder="文件状态" class="global-search-box"/>
      <el-input v-model="searchObj.data.version" size="medium" placeholder="文件版本" class="global-search-box"/>
      <el-input v-model="searchObj.data.professionclass" size="medium" placeholder="专业类别" class="global-search-box"/>
      <el-input v-model="searchObj.data.contractor" size="medium" placeholder="承包商" class="global-search-box"/>
      <el-input v-model="searchObj.data.area" size="medium" placeholder="厂房区域" class="global-search-box"/>
      <el-select v-model="searchObj.data.consistency" size="medium" placeholder="一致性" class="global-search-box">
        <el-option :value="true" label="一致"/>
        <el-option :value="false" label="不一致"/>
      </el-select>
      <!--<el-date-picker
        v-model="searchObj.data.starttime"
        type="date"
        size="medium"
        value-format="yyyy-MM-dd"
        placeholder="开始日期"
        class="contractor-date"
      />
      <el-input v-model="searchObj.data.wingmanager" size="medium" placeholder="联队负责人" class="global-search-box"/>
      <el-input v-model="searchObj.data.conprincipal" size="medium" placeholder="承包商负责人" class="global-search-box"/>
      <el-input v-model="searchObj.data.scope" size="medium" placeholder="范围" class="global-search-box"/>
      <el-input v-model="searchObj.data.constraints" size="medium" placeholder="制约因素" class="global-search-box"/>-->
      <el-button type="primary" size="medium" icon="el-icon-search" @click="getContractors()">搜索</el-button>
      <el-button type="primary" size="medium" @click="reset()">重置</el-button>

      <el-button type="primary" size="medium" icon="el-icon-plus" @click="addContractor">新增</el-button>
      <el-button type="danger" size="medium" icon="el-icon-close" @click="deleteContractors">删除</el-button>

    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      fit
      border
      highlight-current-row
      @selection-change="handleSelectChange">
      <el-table-column type="selection" width="40"/>
      <el-table-column align="center" label="识别码" prop="id" sortable min-width="102"/>
      <el-table-column align="center" label="方案名称" prop="schemename" sortable min-width="160"/>
      <el-table-column align="center" label="方案类别" prop="schemeclass" sortable min-width="102"/>
      <el-table-column align="center" label="文件编码" prop="serialnumber" sortable min-width="160">
        <template slot-scope="scope">
          <!--<a
            :href="'http://10.246.11.7:8610/webtop/drl/objectId/'+ objIds[scope.row.id]"
            target="_blank"
            class="serial-number"
            @click="isHref(scope.row.id, $event)">
            <span>{{ scope.row.serialnumber }}</span>
          </a>-->
          <span class="serial-number" @click="getFileInfo(scope.row.serialnumber)">{{ scope.row.serialnumber }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="文件状态" prop="state" sortable min-width="102"/>
      <el-table-column align="center" label="文件版本" prop="version" sortable min-width="102"/>
      <el-table-column align="center" label="专业类别" prop="professionclass" sortable min-width="102"/>
      <el-table-column align="center" label="承包商" prop="contractor" sortable min-width="90"/>
      <el-table-column align="center" label="厂房区域" prop="area" sortable min-width="102"/>
      <el-table-column align="center" label="一致性" min-width="102">
        <template slot-scope="scope">
          <span>{{ scope.row.consistency ? '是': '否' }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="一致性分析" prop="consistency_info" min-width="120"/>
      <el-table-column align="center" label="变更日志" min-width="102">
        <template slot-scope="scope">
          <span type="primary" class="serial-number" size="mini" @click="showLog(scope.row.id)">查看</span>
        </template>
      </el-table-column>
      <!--<el-table-column align="center" label="联队负责人" prop="wingmanager" sortable min-width="120"/>
      <el-table-column align="center" label="范围" prop="scope" sortable min-width="100"/>
      <el-table-column align="center" label="承包负责人" prop="conprincipal" sortable min-width="120"/>
      <el-table-column align="center" label="制约因素类别" prop="constraints" sortable min-width="130"/>
      <el-table-column align="center" label="备注" prop="remark" sortable min-width="260"/>
      <el-table-column align="center" label="修改者" prop="mender" sortable min-width="90"/>
      <el-table-column align="center" label="创建者" prop="creator" sortable min-width="90"/>
      <el-table-column align="center" label="开始时间" prop="starttime" sortable min-width="103"/>
      <el-table-column align="center" label="创建日期" prop="create_date" sortable min-width="103"/>
      <el-table-column align="center" label="修改日期" prop="modify_date" sortable min-width="103"/>-->
      <el-table-column align="center" label="操作" min-width="280">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" icon="el-icon-edit" @click="editContractor(scope.row)">修改</el-button>
          <el-button type="danger" size="mini" icon="el-icon-delete" @click="deleteRole(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :title=" isEdit ? '编辑资源' : '新增资源'" :visible.sync="FormVisible" width="700px">
      <el-form ref="ruleForm" :model="contractorForm" :rules="rules">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item :label-width="formLabelWidth" label="ID" prop="id">
              <el-input v-model="contractorForm.id" :disabled="isEdit" auto-complete="off"/>
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" label="承包商" prop="contractor">
              <el-input v-model="contractorForm.contractor" auto-complete="off"/>
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" label="方案名称" prop="schemename">
              <el-input v-model="contractorForm.schemename" auto-complete="off"/>
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" label="文件编码" prop="serialnumber">
              <el-input v-model="contractorForm.serialnumber" auto-complete="off"/>
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" label="厂房区域">
              <el-input v-model="contractorForm.area" auto-complete="off"/>
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" label="专业类别">
              <el-input v-model="contractorForm.professionclass" auto-complete="off"/>
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" label="方案类别">
              <el-input v-model="contractorForm.schemeclass" auto-complete="off"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label-width="formLabelWidth" label="版本">
              <el-input v-model="contractorForm.version" auto-complete="off"/>
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" label="状态">
              <el-input v-model="contractorForm.state" auto-complete="off"/>
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" label="范围">
              <el-input v-model="contractorForm.scope" auto-complete="off"/>
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" label="开始时间" prop="starttime">
              <el-date-picker
                v-model="contractorForm.starttime"
                type="date"
                size="medium"
                value-format="yyyy-MM-dd"
                placeholder="开始时间"
                class="contractor-date"
              />
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" label="联队负责人">
              <el-input v-model="contractorForm.wingmanager" auto-complete="off"/>
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" label="承包商负责人">
              <el-input v-model="contractorForm.conprincipal" auto-complete="off"/>
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" label="制约因素类别">
              <el-input v-model="contractorForm.constraints" auto-complete="off"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item :label-width="formLabelWidth" label="备注">
          <el-input v-model="contractorForm.remark" type="textarea" auto-complete="off"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelAdd()">取 消</el-button>
        <el-button
          type="primary"
          @click="isEdit ? confirmEditContractor('ruleForm') : confirmAddContractor('ruleForm')">确 定
        </el-button>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="showFile" title="文件信息" width="500px" class="dialog-file-info">
      <div v-loading="fileLoading" v-if="fileInfo !== null" style="margin: -30px 0;">
        <p><span>文件名称</span><span>{{ fileInfo.c_title }}</span></p>
        <p><span>文件编码</span><span>{{ fileInfo.c_doc_number }}</span></p>
        <p><span>文件版本</span><span>{{ fileInfo.c_revision }}</span></p>
        <p><span>文件状态</span><span>{{ fileInfo.c_doc_status }}</span></p>
        <p><span>是否最新</span><span>{{ fileInfo.c_latest_version }}</span></p>
      </div>
      <div v-else style="text-align: center;color: #999999">提示：没有相关文件</div>
      <div v-if="fileInfo !== null" slot="footer" class="dialog-footer" style="text-align: center">
        <a
          :href="'http://10.246.11.94:8081/document/download/'+ fileInfo.r_object_id"
          target="_blank"
          class="serial-number">
          <span>下载</span>
        </a>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="showLogDialog" center title="日志信息" width="610px" class="dialog-file-info">
      <div v-if="logInfo.length > 0" style="margin-top: -10px">
        <el-table
          v-loading="showLogLoading"
          :data="logInfo"
          element-loading-text="Loading"
          fit
          border
          highlight-current-row>
          <el-table-column align="center" label="日志ID" prop="id" min-width="100"/>
          <el-table-column align="center" label="变更前版本" prop="previous_version" min-width="140"/>
          <el-table-column align="center" label="变更后版本" prop="current_version" min-width="140"/>
          <el-table-column align="center" label="变更日期" prop="create_date" min-width="160"/>
        </el-table>
      </div>
      <div v-else style="text-align: center;color: #999999">提示：没有相关日志</div>
    </el-dialog>
    <div class="global-paging">
      <el-pagination
        :page-size="10"
        :total="total"
        :page-sizes="[10, 20, 40]"
        background
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"/>
    </div>
  </div>
</template>

<script>
import commonApi from '@/api/commonApi'

export default {
  name: 'ContractorList',
  data() {
    return {
      FormVisible: false,
      showFile: false,
      showLogDialog: false,
      showLogLoading: false,
      fileLoading: false,
      isEdit: false,
      list: null,
      fileInfo: {},
      logInfo: [],
      listLoading: false,
      total: null,
      rules: {
        id: [
          { required: true, message: '请输入ID', trigger: 'blur' }
        ],
        contractor: [
          { required: true, message: '请输入承包商', trigger: 'blur' }
        ],
        schemename: [
          { required: true, message: '请输入方案名称', trigger: 'blur' }
        ],
        serialnumber: [
          { required: true, message: '请输入文件编码', trigger: 'blur' }
        ],
        starttime: [
          { required: true, message: '请选择日期', trigger: 'change' }
        ]
      },
      delContractorIds: [],
      formLabelWidth: '100px',
      searchObj: {
        currentNo: 1,
        pageSize: 10,
        data: {
          id: ''
        }
      },
      contractorForm: {},
      copyContractorForm: {}, // 修改取消时使用
      url: '',
      objIds: {}
    }
  },
  created() {
    this.getContractors()
  },
  methods: {
    /* isHref(id, e) {
      if (!this.objIds[id]) {
        e.preventDefault()
        this.$alert('该文档在Documentnum系统中不存在。', '温馨提示', {
          confirmButtonText: '确定',
          type: 'warning'
        })
      }
    },
    getObjectId(i) {
      commonApi('/constractor/getDocumentByNumber/' + this.list[i].serialnumber, 'get').then(response => {
        if (response.code === 1) {
          this.objIds[this.list[i].id] = response.data
          i++
          if (i === this.list.length) {
            return
          }
          this.getObjectId(i)
        }
      })
    },*/
    getFileInfo(code) {
      this.showFile = true
      this.fileLoading = true
      this.fileInfo = {}
      commonApi('http://10.246.11.94:8081/document/details/' + code, 'get').then(response => {
        if (response.code === 1) {
          this.fileInfo = response.data
          this.fileLoading = false
        }
      })
    },
    showLog(c_id) {
      this.showLogDialog = true
      this.showLogLoading = true
      commonApi(`/constractor/changeLogList/${c_id}`, 'get').then(response => {
        if (response.code === 1) {
          this.logInfo = response.data
          this.showLogLoading = false
        }
      })
    },
    handleCurrentChange(val) {
      this.searchObj.currentNo = val
      this.getContractors()
    },
    handleSizeChange(val) {
      this.searchObj.pageSize = val
      this.getContractors()
    },
    getContractors() {
      this.listLoading = true
      commonApi('/constractor/pages', 'post', this.searchObj).then(response => {
        if (response.code === 1) {
          this.list = response.data.rows
          this.total = response.data.total
          this.listLoading = false
          // this.getObjectId(0)
        }
      }).catch(() => {
        this.listLoading = false
      })
    },
    reset() {
      for (const index in this.searchObj.data) {
        this.searchObj.data[index] = null
      }
      this.getContractors()
    },
    handleSelectChange(valArr) {
      this.delContractorIds = []
      for (let i = 0; i < valArr.length; i++) {
        this.delContractorIds.push(valArr[i].id)
      }
    },
    addContractor() {
      this.isEdit = false
      this.FormVisible = true
    },
    confirmAddContractor(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.listLoading = true
          commonApi('/constractor/add', 'post', this.contractorForm).then(response => {
            if (response.code === 1) {
              this.contractorForm = {}
              this.listLoading = false
              this.FormVisible = false
              this.getContractors()
            }
          }).catch(() => {
            this.listLoading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    cancelAdd() {
      this.FormVisible = false
      if (!this.isEdit) {
        for (const index in this.contractorForm) {
          this.contractorForm[index] = null
        }
      } else {
        this.contractorForm = this.copyContractorForm
      }
    },
    deleteRole(row) {
      this.$confirm(`确定要删除承包商：${row.contractor} ?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.delContractorIds.push(row.id)
        this.deleteContractorsInterim()
      }).catch(() => {
      })
    },
    deleteContractors() {
      if (this.delContractorIds.length === 0) {
        this.$message({
          message: '请选择需要删除承包商 ！',
          type: 'error'
        })
        return
      }
      this.$confirm(`确定要批量删除承包商 ?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
      }).then(() => {
        this.deleteContractorsInterim()
      }).catch(() => {
      })
    },
    deleteContractorsInterim() {
      this.listLoading = true
      commonApi('/constractor/remove', 'post', this.delContractorIds).then(response => {
        if (response.code === 1) {
          this.$message({
            message: '承包商删除完成',
            type: 'success'
          })
          this.listLoading = false
          this.delContractorIds = []
          this.getContractors()
        }
      }).catch(() => {
        this.listLoading = false
      })
    },
    editContractor(row = {}) {
      this.FormVisible = true
      this.isEdit = true
      this.contractorForm = JSON.parse(JSON.stringify(row))
      this.copyContractorForm = JSON.parse(JSON.stringify(row))
    },
    confirmEditContractor(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.FormVisible = false
          this.listLoading = true
          commonApi('/constractor/update', 'post', this.contractorForm).then(response => {
            if (response.code === 1) {
              this.contractorForm = {}
              this.listLoading = false
              this.getContractors()
            }
          }).catch(() => {
            this.listLoading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>
<style lang="scss">
  .filter-container-more{
    background-color: white;
    margin-bottom: 10px;
    padding:10px 20px 0;
  }
  .filter-container-more .global-search-box{
    padding-bottom: 10px
  }
  .contractor-date {
    width: 170px;
  }

  .serial-number {
    color: #409EFF;
    cursor: pointer;
  }
  .serial-number:hover {
    color: green;
  }
  .dialog-file-info p span{
    margin: 0 30px;
  }
</style>
